package io.github.dunwu.javacore.jdk8.nashorn;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/**
 * Bind java objects to custom javascript objects.
 *
 * @author Benjamin Winterberg
 */
public class Nashorn5 {

	public static void main(String[] args) throws Exception {
		ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
		engine.eval("load('res/nashorn5.js')");

		Invocable invocable = (Invocable) engine;

		Product product = new Product();
		product.setName("Rubber");
		product.setPrice(1.99);
		product.setStock(1037);

		Object result = invocable.invokeFunction("getValueOfGoods", product);
		System.out.println(result);
	}

}
